<template>
  <div class="container">
    <customSpace fullWidth    style="padding: 5px 10px">
      <template #content>
        <customButton  title="新增资质"  v-if="!cpReadonly" theme="success"  @click="certOp('add','新增资质')" :disabled="!cpIsSelect" helpDisabled="请先选择下方节点"/>
        <customButton  title="查看资质"  theme="primary" @click="certOp('viewList','查看资质')" :disabled="!cpIsSelect" helpDisabled="请先选择下方节点"/>
        <span >
            <small class="span-tip" v-if="queryType == 'warn'">
                仅显示已审核，未作废的已过期资质或者效期在30天以内的
              </small>
              <small class="span-tip" v-if="queryType == 'view'">
                仅显示已审核，并且未作废的资质
              </small>
              <small class="span-tip" v-if="queryType == 'crud'">
                当前节点 <span v-html="cpPropSubtitle"></span>
              </small>
          </span>
      </template>
    </customSpace>

    <!--左侧树-->
    <treeCert ref="treeCert" class="tree-cert" @click="handleTreeClick"   :queryType="queryType"  @afterLoad="afterLoadTree"/>

    <!--资质查看列表-->
    <customDrawer  title="资质列表"
                   :subtitle="cpPropSubtitle"
                   ref="customDrawerCertDtl"
                   :autoHide="false"
                   p94Only
                   placement="right"
                   :footer="null"
                   @close="certDtlClose"
                   class="t-body-overflow-y-hide"
    >
      <template #content>
        <customForm :labelWidth="60" style="padding: 5px 0px">
          <template #content>
            <customButton  title="新增资质"  v-if="!cpReadonly" theme="success"  @click="certOp('add','新增资质')" :disabled="!cpIsSelect" helpDisabled="请先选择下方节点"/>
            <customFormItem label="级联展示" name="cascade" :parent="_self" :form="formDtl"  :type="constCommon.formItemType.switch.name" @change="loadDtl"
                            help="选中后，将显示当前节点的所有子级数据"
            />
            <small class="span-tip" style="    position: absolute;right: 14px;">
              勾选后，显示包含字节点数据
            </small>

          </template>
        </customForm>

        <DataList
          ref="certList"
          :propAutoFetchData="false"
          :propSearchFields="['certificateName','certificateNo']"
          :propSearchTextAppend="'资质名称/资质编号'"
          propUrl="/cert/certificate_repositorys/pageWarn"
          :showFieldList="cpShowFieldList"
          :vanFieldLabelWith5="true"
          :sortDefault="{field: 'gmtModified',type: 'desc'}"
          @click="itemClick"
          :propNoCheck="true"
          :propUserParam="{queryType: queryType}"
        >
        </DataList>
      </template>
    </customDrawer>


    <!--资质维护-->
    <customDrawer
      title="资质详情"
      :subtitle="cpCertFormSubtitle"
      ref="customDrawerCertDetail"
      :autoHide="false"
      p94Only
      placement="right"
      :footer="null"
      @close="certDetailClose"
    >
      <template #content>
        <certDetail ref="certDetail" :propInitData="selectedNode" @close="certDetailClose"  :propReadonly="cpReadonly"></certDetail>
      </template>
    </customDrawer>
  </div>
</template>

<script>
import treeCert from '@/components/custom/bus/treeCert.vue'
import certDetail from '@/views/app/cert/sp_cert_detail.vue'

export default {
  help: '资质通用列表-列表',
  components: {
    treeCert, certDetail
  },

  props:['queryType'],
  data() {
    return {
      //锁选中的节点
      selectedNode: {},

      // 所展示的资质明细
      certRowShow: {},
      formDtl: {
        cascade: false
      }
    }
  },
  computed: {
    // 弹框子标题
    cpPropSubtitle() {
      if (!this.selectedNode.id) {
        return ''
      }
      return global.utils.getDictLabel(
        this.constCommon.certTypeNameMap[this.selectedNode.typeName].businessTypeCode,
        global.constCommon.dictType.certBusinessType,
        '',
        true)
    },
    // 资质编辑窗口子标题
    cpCertFormSubtitle() {
      if (!this.selectedNode.id) {
        return ''
      }
      if (this.certRowShow && this.certRowShow.businessTypeCode) {
        return global.utils.getDictLabel(
          this.certRowShow.businessTypeCode,
          global.constCommon.dictType.certBusinessType,
          '',
          true)
      }
      return global.utils.getDictLabel(
        this.constCommon.certTypeNameMap[this.selectedNode.typeName].businessTypeCode,
        global.constCommon.dictType.certBusinessType,
        '',
        true)
    },
    // 是否选中节点
    cpIsSelect() {
      return this.strings.isNotBlank(this.selectedNode.id)
    },
    cpShowFieldList() {
      /** 配置说明
       ,sortDisabled: true 不可排序
       ,filterDisabled: true  更多搜索时不展示
       ,filterOrder: 1  更多搜索字段显示顺序，不指定则按照传入字段顺序，可以只改某些字段顺序为负数，这样就可以排前面
       ,showDisabled: true  列表内容区域不显示
       ,showFormOpenDisabled: true  详情表单不显示
       ,showAllDisabled: true   表格、表单、搜索都不显示
       **/

      let fieldList = [
        { label: '资质所属', name: 'certExtName', span: 24, filterDisabled: true, isTitle: true },
        {
          label: '资质类型', name: 'businessTypeCode', span: 12, isTitleSub: true,
          queryType: this.constCommon.formItemType.selectDict.name,
          queryConfig: {
            dictType: this.constCommon.dictType.certBusinessType,
            multiple: true
          }
        },

        { label: '资质编号', name: 'certificateNo', span: 24 },
        { label: '效期开始', name: 'expiryStartDate', span: 24, filterDisabled: true },
        {
          label: '效期截止',
          name: 'expiryDate',
          span: 24,
          queryType: this.constCommon.formItemType.datePickerBetween.name
        },

        {
          label: '已提交', name: 'submitFlag', span: 12, flagBoolean: true,
          queryType: this.constCommon.formItemType.switch.name,
          queryConfig: {
            dictType: this.constCommon.dictType.commonYesNo
          }
        },
        {
          label: '已作废', name: 'closeFlag', span: 12, flagBoolean: true,
          queryType: this.constCommon.formItemType.switch.name,
          queryConfig: {
            dictType: this.constCommon.dictType.commonYesNo
          }
        },
        {
          label: '已过期', name: 'flagDateOver', span: 12, flagBoolean: true,
          queryType: this.constCommon.formItemType.switch.name,
          queryConfig: {
            dictType: this.constCommon.dictType.commonYesNoReverse
          }
        },
        { label: '过期天数', name: 'dayOver', span: 12, filterDisabled: true },
        { label: '版本', name: 'version', span: 12, filterDisabled: true },
        { label: '修改时间', name: 'gmtModified', span: 24, filterDisabled: true }
      ]
      fieldList = [...fieldList, ...this.constCommon.filterCommon.sp()]
      return fieldList
    },
    cpReadonly() {
      return 'crud' != this.queryType
    }
  },
  mounted() {
  },
  methods: {
    handleTreeClick(node) {
      this.selectedNode = node
    },
    afterLoadTree(node) {
      this.selectedNode = node
    },
    loadTree() {
      this.$refs.treeCert.load()
    },
    // 加载数据
    loadDtl() {
      let wrapper = this.$newWrapper()
      wrapper.eq('close_flag', 0)
      if (this.selectedNode.id) {
        wrapper.eq('supplier_id', this.selectedNode.spId)
        //非级联，只差当前节点
        if (!this.formDtl.cascade) {
          wrapper.eq('target_describe_id', this.selectedNode.targetDescribeId)
        }

        let typeName = this.selectedNode.typeName
        if (typeName == this.constCommon.certTypeNameMap.mfg.key) { //厂商节点
          wrapper.ne('business_type_code', this.constCommon.certTypeNameMap.sp.businessTypeCode)
          wrapper.eq('manufacturer_id', this.selectedNode.mfgId)
        } else if (typeName == this.constCommon.certTypeNameMap.cat.key) { // 品类节点
          wrapper.notIn('business_type_code', [this.constCommon.certTypeNameMap.sp.businessTypeCode,this.constCommon.certTypeNameMap.mfg.businessTypeCode])
          wrapper.eq('manufacturer_id', this.selectedNode.mfgId)
          wrapper.eq('catalog_id', this.selectedNode.catId)
        }else if (typeName == this.constCommon.certTypeNameMap.matr.key) { // 商品节点
          wrapper.eq('business_type_code', this.constCommon.certTypeNameMap.matr.businessTypeCode)
          wrapper.eq('manufacturer_id', this.selectedNode.mfgId)
          wrapper.eq('catalog_id', this.selectedNode.catId)
          wrapper.eq('material_id', this.selectedNode.matrId)
        }
      } else {
        wrapper.eq('1', '-1')
      }
      this.$refs.certList.setQueryWrapper(wrapper)
    },

    // 资质列表关闭后
    certDtlClose() {
      if (this.cpReadonly) {
        return
      }
      this.loadTree()
    },

    // 资质维护关闭后
    certDetailClose() {
      if (this.cpReadonly) {
        return
      }
      //资质列表已打开，只刷新列表
      if (this.$refs.customDrawerCertDtl.visible) {
        this.loadDtl()
      }else {
        this.$refs.customDrawerCertDetail.hide()
        this.loadTree()
      }
    },

    certOp(type){
      switch (type) {
        case 'add': { // 新增资质
          if (!this.selectedNode.id) {
            this.utilMessage.warning('请选择左侧需要添加的节点')
            return
          }
          switch (this.selectedNode.typeName) {
            //品类节点
            case 'cat': {
              if (this.selectedNode.catId == '0') {
                this.utilMessage.warning('未分类品类不需要维护资质')
                return
              }
            }
          }
          this.certRowShow = null
          this.$refs.customDrawerCertDetail.show()
          this.$refs.certDetail.initForm()
          break
        }
        case 'viewList': {// 查看资质列表
          this.$refs.customDrawerCertDtl.show()
          this.loadDtl()
          break
        }
      }
    },

    // 资质列表点击
    itemClick(item) {
      this.certRowShow = item
      this.$refs.customDrawerCertDetail.show()
      this.$refs.certDetail.initForm(this.certRowShow)
    }
  },
  watch: {
  }
}
</script>


<style lang="scss" scoped>
.tree-cert{
  width: 96% !important;
  margin-left: 2% !important;
}
</style>
